package com.brite.android.game.menu;

import java.util.ArrayList;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Display;
import android.view.KeyEvent;

import com.brite.android.game.Wellcome;
import com.brite.android.game.action.Cordinate;
import com.brite.android.game.action.Region;

public class SettingActivity extends BaseGameActivity {

	private int CAMERA_WIDTH;
	private int CAMERA_HEIGHT;
	
	private Scene scene;
	
	private Texture bgTexture;
	private TextureRegion bgTextureRegion;
	
	private Texture soundTexture;
	private TiledTextureRegion soundTextureRegion;
	
	private Texture musicTexture;
	private TiledTextureRegion musicTextureRegion;
	
	private Texture vibrateTexture;
	private TiledTextureRegion vibrateTextureRegion;
	
	private Texture backTexture;
	private TiledTextureRegion backTextureRegion;
	
	private ArrayList<AnimatedSprite> arrListOptions = new ArrayList<AnimatedSprite>();
	private boolean[] settingsHolder = new boolean[3];
	private int settingRotate = 1;
	
	@Override
	public Engine onLoadEngine() {
		Display display = getWindowManager().getDefaultDisplay();
		CAMERA_WIDTH = display.getWidth();
		CAMERA_HEIGHT = display.getHeight();
		
		Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions eo = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
		eo.setNeedsMusic(true);
		eo.setNeedsSound(true);
		mEngine = new Engine(eo);
		
		return mEngine;
	}

	private void loadOptionResources(){
		soundTexture = new Texture(128, 128, TextureOptions.DEFAULT);
		soundTextureRegion = TextureRegionFactory.createTiledFromAsset(soundTexture, this, "soundOptional.png", 0, 0, 2, 1);
		
		musicTexture = new Texture(128, 128, TextureOptions.DEFAULT);
		musicTextureRegion = TextureRegionFactory.createTiledFromAsset(musicTexture, this, "musicOptional.png", 0, 0, 2, 1);
		
		vibrateTexture = new Texture(128, 128, TextureOptions.DEFAULT);
		vibrateTextureRegion = TextureRegionFactory.createTiledFromAsset(vibrateTexture, this, "vibrateOptional.png", 0, 0, 2, 1);
		
		backTexture = new Texture(128, 128, TextureOptions.DEFAULT);
		backTextureRegion = TextureRegionFactory.createTiledFromAsset(backTexture, this, "backOptional.png", 0, 0, 1, 1);
		
		mEngine.getTextureManager().loadTextures(soundTexture, musicTexture, vibrateTexture, backTexture);
	}
	
	@Override
	public void onLoadResources() {
		TextureRegionFactory.setAssetBasePath("gfx/");
		
		bgTexture = new Texture(1024, 1024, TextureOptions.DEFAULT);
		bgTextureRegion = TextureRegionFactory.createFromAsset(bgTexture, this, "bg_MenuSetting.png", 0, 0);

		loadOptionResources();
		
		mEngine.getTextureManager().loadTextures(bgTexture);
	}

	private void loadOptional(){
		final AnimatedSprite soundAnimatedSprite = new AnimatedSprite(0, 0, soundTextureRegion);
		scene.attachChild(soundAnimatedSprite);
		
		final AnimatedSprite musicAnimatedSprite = new AnimatedSprite(0, 0, musicTextureRegion);
		musicAnimatedSprite.setPosition(0, soundAnimatedSprite.getY() + soundAnimatedSprite.getHeightScaled() + 10);
		scene.attachChild(musicAnimatedSprite);
		
		final AnimatedSprite vibrateAnimatedSprite = new AnimatedSprite(0, 0, vibrateTextureRegion);
		vibrateAnimatedSprite.setPosition(0, musicAnimatedSprite.getY() + musicAnimatedSprite.getHeightScaled() + 10);
		scene.attachChild(vibrateAnimatedSprite);
		
		final AnimatedSprite backAnimatedSprite = new AnimatedSprite(0, 0, backTextureRegion);
		backAnimatedSprite.setPosition(0, vibrateAnimatedSprite.getY() + vibrateAnimatedSprite.getHeightScaled() + 10);
		scene.attachChild(backAnimatedSprite);
		
		arrListOptions.add(soundAnimatedSprite);
		arrListOptions.add(musicAnimatedSprite);
		arrListOptions.add(vibrateAnimatedSprite);
		arrListOptions.add(backAnimatedSprite);
	}
	
	@Override
	public Scene onLoadScene() {
		scene = new Scene(1);
		
		Sprite bgSprite = new Sprite(0, 0, bgTextureRegion);
		bgSprite.setWidth(CAMERA_WIDTH);
		bgSprite.setHeight(CAMERA_HEIGHT);
		scene.attachChild(bgSprite);	
		
		loadOptional();
		
		getSettings();
		
		scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
			
			@Override
			public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
				
				for (int i = 0; i < arrListOptions.size(); i++) {
					AnimatedSprite optionSprite = arrListOptions.get(i);
					
					Region optionRegion = new Region(optionSprite.getX(), optionSprite.getY(), optionSprite.getWidthScaled(), optionSprite.getHeightScaled());
					if (pSceneTouchEvent.isActionDown() && optionRegion.isCovered(new Cordinate(pSceneTouchEvent.getX(), pSceneTouchEvent.getY()), optionRegion)) {
						if (i != arrListOptions.size() - 1) {
							optionSprite.setCurrentTileIndex(optionSprite.getCurrentTileIndex() == 0 ? 1 : 0);
							settingsHolder[i] = (settingsHolder[i] == true ? false : true);
							break;
						}else if(i == arrListOptions.size() - 1){
							saveSetting();
							startActivity(new Intent(getBaseContext(), MainMenu.class));
							finish();
							return true;
						}
					}
				}
				
				return false;
			}
		});
		
		return scene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			startActivity(new Intent(this, MainMenu.class));
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void getSettings() {
		SharedPreferences settings = getSharedPreferences(Wellcome.PREFS_NAME, MODE_PRIVATE);
		
		settingRotate = settings.getInt("rotate", 2);
		settingsHolder[0] = settings.getBoolean("sound", false);
		settingsHolder[1] = settings.getBoolean("music", false);
		settingsHolder[2] = settings.getBoolean("vibrate", false);
		
		for (int i = 0; i < arrListOptions.size()-1; i++) {
			arrListOptions.get(i).setCurrentTileIndex(settingsHolder[i] == true ? 0 : 1);
		}
	}
	
	private void saveSetting(){
		SharedPreferences settings = getSharedPreferences(Wellcome.PREFS_NAME, MODE_PRIVATE);
	    SharedPreferences.Editor editor = settings.edit();
		editor.putInt("rotate", settingRotate);
		editor.putBoolean("sound", settingsHolder[0]);
		editor.putBoolean("music", settingsHolder[1]);
		editor.putBoolean("vibrate", settingsHolder[2]);
		editor.commit();
	}
	
}
